package com.zadtek.shopmall.activities;

import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.zadtek.shopmall.R;
import com.zadtek.shopmall.base.AbsActivity;
import com.zadtek.shopmall.fragments.ProductDetailFragment;
import com.zadtek.shopmall.fragments.ProductDetailInfoFragment;
import com.zadtek.shopmall.fragments.ProductEvaluationFragment;

import java.util.LinkedList;
import java.util.List;

public class ProductDetailActivity extends AbsActivity implements View.OnClickListener {
    private ImageView ivBack;
    private TabLayout tabLayout;
    private ViewPager viewPager;

    private TabPageAdapter tabPageAdapter;
    private List<String> titles;
    private List<Fragment> fragments;



    @Override
    public int setView() {
        return R.layout.activity_product_detail;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        initView();
        initBaseData();
        initLogic();
    }

    private void initBaseData() {
        titles = new LinkedList<>();
        titles.add("商品");
        titles.add("详情");
        titles.add("评论");

        fragments = new LinkedList<>();
        fragments.add(new ProductDetailFragment());
        fragments.add(new ProductDetailInfoFragment());
        fragments.add(new ProductEvaluationFragment());

    }

    private void initView() {
        ivBack =(ImageView) findViewById(R.id.ic_back);
        ivBack.setVisibility(View.VISIBLE);
        tabLayout = (TabLayout) findViewById(R.id.tablayout);
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        tabLayout.setupWithViewPager(viewPager);


    }
    private void initLogic() {
        tabPageAdapter = new TabPageAdapter(getSupportFragmentManager(),fragments,titles);
        viewPager.setAdapter(tabPageAdapter);
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){

        }
    }

    public void setCurrentPage(int page) {
        viewPager.setCurrentItem(page);
    }

    private class TabPageAdapter extends FragmentPagerAdapter {
        private List<Fragment> fragments;
        private List<String> titles;

        public TabPageAdapter(FragmentManager fm, List<Fragment> fragmentList, List<String> stringList) {
            super(fm);
            fragments = fragmentList;
            titles = stringList;
        }
        @Override
        public Fragment getItem(int position) {
            return fragments.get(position);
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }
}
